<template>
  <div>

    <!-- 面包屑导航条 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信任设备管理</el-breadcrumb-item>
      <el-breadcrumb-item>场景规则管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 头部的搜索和添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryinfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getSceneList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true;addForm={}">添加场景规则</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table
        :data="sceneList"
        border
        stripe
        style="width: 100%">
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          prop="sceneCode"
          label="场景编码"
          width="70">
        </el-table-column>
        <el-table-column
          prop="sceneName"
          label="场景名称" width="100">
        </el-table-column>
        <el-table-column
          prop="sceneDesc"
          label="场景描述" width="100">
        </el-table-column>


        <el-table-column label="金额上限 " prop="amountUpperLimmit">
        </el-table-column>
        <el-table-column label="金额下限 " prop="amountLowerLimmit">
        </el-table-column>

        <el-table-column
          prop="invokeRequestUri"
          label="请求uri" width="300">
        </el-table-column>


        <el-table-column label="用户类型 " prop="userFlag">
        </el-table-column>

        <el-table-column label="用户等级 " prop="userType">

        </el-table-column>



        <el-table-column label="校验规则 " prop="ruleCodeList">

        </el-table-column>




        <el-table-column label="是否信任设备" prop="isTrustDevice">
          <el-switch
            v-model="scope.row.isTrustDevice"
            @change="switchChanged2(scope.row.isTrustDevice,'isTrustDevice', scope.row.id)"
            slot-scope="scope">
          </el-switch>
        </el-table-column>


        <el-table-column
          label="状态">
          <!-- 让这一行的开关，把状态绑定到 这一行数据的 mg_state 上 -->
          <!-- <el-switch
            v-model="scope.row.mg_state"
            @change="(newState) => {switchChanged(newState, scope.row.id)}"
            slot-scope="scope">
          </el-switch> -->
          <el-switch
            v-model="scope.row.state"
            @change="switchChanged2(scope.row.state,'state', scope.row.id)"
            slot-scope="scope">
          </el-switch>
        </el-table-column>


        <el-table-column
          width="185"
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="remove(scope)"></el-button>
            <el-tooltip effect="dark" content="配置校验规则" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRuleDialog(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加规则的对话框 -->
    <el-dialog
      title="添加规则"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed">
      <!-- 添加用户的表单 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">

        <el-form-item label="场景名称: " >
          <el-select v-model="addForm.sceneCode" placeholder="请选择">
            <!-- el-option 中： -->
            <!-- label 控制的是显示的名称 -->
            <!-- value 控制的是某一项被选中以后，选中的真正的值 -->
            <el-option
              v-for="item in dictList"
              :key="item.id"
              :label="item.sceneName"
              :value="item.sceneCode">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="场景描述: " prop="sceneDesc" >
          <el-input v-model="addForm.sceneDesc"></el-input>
        </el-form-item>
        <el-form-item label="请求uri: " prop="invokeRequestUri">
          <el-input v-model="addForm.invokeRequestUri"></el-input>
        </el-form-item>


        <el-form-item label="金额上限: " prop="amountUpperLimmit">
          <el-input-number v-model="addForm.amountUpperLimmit" :step="1"></el-input-number>
        </el-form-item>
        <el-form-item label="金额下限: " prop="amountLowerLimmit">
          <el-input-number v-model="addForm.amountLowerLimmit" :step="1"></el-input-number>
        </el-form-item>




        <el-form-item label="用户类型: " prop="userFlag">
          <el-radio-group v-model="addForm.userFlag">
            <el-radio-button label="0" >个人</el-radio-button>
            <el-radio-button label="1" >企业</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="用户等级: " prop="userType">
          <el-radio-group v-model="addForm.userType">
            <el-radio-button label="0" >低门槛</el-radio-button>
            <el-radio-button label="2" >高门槛</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态: " prop="state">
          <el-switch
            v-model="addForm.state"
            slot-scope="scope">
          </el-switch>
        </el-form-item>

        <el-form-item label="是否信任设备" prop="isTrustDevice">
          <el-switch
            v-model="addForm.isTrustDevice"
            slot-scope="scope">
          </el-switch>
        </el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addScene">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户的对话框 -->
    <el-dialog
      title="修改规则信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed">
      <!-- 编辑用户的表单 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="110px">

        <el-form-item label="场景编码" prop="sceneCode">
          <el-input v-model="editForm.sceneCode" disabled></el-input>
        </el-form-item>
        <el-form-item label="场景名称" prop="sceneName">
          <el-input v-model="editForm.sceneName"></el-input>
        </el-form-item>
        <el-form-item label="场景描述" prop="sceneDesc">
          <el-input v-model="editForm.sceneDesc"></el-input>
        </el-form-item>
        <el-form-item label="请求uri" prop="invokeRequestUri">
          <el-input v-model="editForm.invokeRequestUri"></el-input>
        </el-form-item>


        <el-form-item label="金额上限: " prop="amountUpperLimmit">
          <el-input-number v-model="editForm.amountUpperLimmit" :step="1"></el-input-number>
        </el-form-item>
        <el-form-item label="金额下限: " prop="amountLowerLimmit">
          <el-input-number v-model="editForm.amountLowerLimmit" :step="1"></el-input-number>
        </el-form-item>




        <el-form-item label="用户类型: " prop="userFlag">
          <el-radio-group v-model="editForm.userFlag">
            <el-radio-button label="0" >个人</el-radio-button>
            <el-radio-button label="1" >企业</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="用户等级: " prop="userType">
          <el-radio-group v-model="editForm.userType">
            <el-radio-button label="0" >低门槛</el-radio-button>
            <el-radio-button label="2" >高门槛</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否信任设备" prop="isTrustDevice">
          <el-switch
            v-model="editForm.isTrustDevice"
            slot-scope="scope">
          </el-switch>
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-switch
            v-model="editForm.state"
            slot-scope="scope">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRule">确 定</el-button>
      </span>
    </el-dialog>


    <!-- 为用户分配角色的对话框 -->
    <el-dialog
      title="分配角色"
      :visible.sync="setRuleDialogVisible"
      width="50%"
      @close="setRoleDialogClosed">
      <!-- 分配角色的表单 -->
      <el-form ref="setRoleFormRef" :model="setRuleForm" label-width="100px">
        <el-form-item label="场景类型：">
          {{setRuleForm.sceneName}}
        </el-form-item>
        <el-form-item label="配置新规则：">
          <!--<el-select v-model="setRuleForm.rids" placeholder="请选择">-->
            <!--&lt;!&ndash; el-option 中： &ndash;&gt;-->
            <!--&lt;!&ndash; label 控制的是显示的名称 &ndash;&gt;-->
            <!--&lt;!&ndash; value 控制的是某一项被选中以后，选中的真正的值 &ndash;&gt;-->
            <!--<el-option-->
              <!--v-for="item in ruleList"-->
              <!--:key="item.id"-->
              <!--:label="item.checkRuleKey"-->
              <!--:value="item.id">-->
            <!--</el-option>-->
          <!--</el-select>-->

          <el-checkbox-group v-model="setRuleForm.rids">
            <el-checkbox-button v-for="rule in ruleList" :label="rule.id" :key="rule.id">{{rule.checkRuleKey}}</el-checkbox-button>
          </el-checkbox-group>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRuleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRule">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import mix from './CheckRule-mixins.js'
  export default {
    mixins: [mix]
  }
</script>

<style lang="less" scoped>
  .red {
    color: red;
  }

  .green {
    color: lightseagreen;
  }

  .zk-table {
    margin-top: 15px;
  }

  .el-cascader {
    width: 100%;
  }
</style>
